package dao;

import model.AlergyDetail;
import model.Doctor;
import model.QandA;
import model.User;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: TM
 * Date: 11/17/12
 * Time: 12:25 PM
 * To change this template use FileModel | Settings | FileModel Templates.
 */
public class AskDocDaoImpl extends JdbcDaoSupport implements AskDocDao{

    public List<QandA> getQandA(final User user){
        try {
            String query = "SELECT \n" +
                    "QUESTION_NO, DOCTOR_ID, NIC, \n" +
                    "   QUESTION, decode(STATUS,0,'PENDING',1,ANSWER) ANSWER, TXN_DATE, \n" +
                    "   STATUS\n" +
                    "FROM ASK_DOCTOR WHERE NIC = ?";
            System.out.println(query+user.getNic());
            return (List<QandA>) getJdbcTemplate().query(query, new Object[]{user.getNic()},
                    new RowMapper() {
                        public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {

                            QandA qanda = new QandA();
                            qanda.setQuestion(resultSet.getString("QUESTION"));
                            qanda.setAnswer(resultSet.getString("ANSWER"));
                            qanda.setDoctorId(resultSet.getString("DOCTOR_ID"));

                            return qanda;
                        }

                    });
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<Doctor> getDoctorList(final User user){
        try {
            String query = "SELECT \n" +
                    "DOCTOR_CODE, DOCTOR_NAME, STATUS, \n" +
                    "   SPECIALTY\n" +
                    "FROM DOCTOR WHERE STATUS = 1";
            System.out.println(query);
            return (List<Doctor>) getJdbcTemplate().query(query,
                    new RowMapper() {
                        public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {

                            Doctor doctor = new Doctor();
                            doctor.setDoctorCode(resultSet.getString("DOCTOR_CODE"));
                            doctor.setDoctorName(resultSet.getString("DOCTOR_NAME"));
                            doctor.setSpecialty(resultSet.getString("SPECIALTY"));

                            return doctor;
                        }

                    });
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }


        public int saveQuestion(final User user, final QandA qanda){
              try {

            String seqQuery = "SELECT SEQ_ASK_DOC.nextval from dual";
                  String sequence = getJdbcTemplate().queryForObject(seqQuery, String.class);

            String query = "INSERT INTO ASK_DOCTOR (\n" +
                    "   QUESTION_NO, DOCTOR_ID, NIC, \n" +
                    "   QUESTION,  TXN_DATE, \n" +
                    "   STATUS) \n" +
                    "VALUES (? ,? ,? ,\n" +
                    "    ?, sysdate , 0 )";
            System.out.println(query);
            return getJdbcTemplate().update(query, new Object[]{sequence, qanda.getDoctorId(),user.getNic() , qanda.getQuestion()});
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return 0;
        }
    }


     public List<QandA> getAnsweredQues(final User user){
        try {
            String query = "SELECT \n" +
                    "QUESTION_NO, DOCTOR_ID, NIC, \n" +
                    "   QUESTION, decode(STATUS,0,'PENDING',1,ANSWER) ANSWER, TXN_DATE, \n" +
                    "   STATUS\n" +
                    "FROM ASK_DOCTOR WHERE NIC = ? AND STATUS = 1 ";
            System.out.println(query+user.getNic());
            return (List<QandA>) getJdbcTemplate().query(query, new Object[]{user.getNic()},
                    new RowMapper() {
                        public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {

                            QandA qanda = new QandA();
                            qanda.setQuestion(resultSet.getString("QUESTION"));
                            qanda.setAnswer(resultSet.getString("ANSWER"));
                            qanda.setDoctorId(resultSet.getString("DOCTOR_ID"));

                            return qanda;
                        }

                    });
        } catch (EmptyResultDataAccessException e) {
            e.printStackTrace();
            return null;
        }
    }
}
